/**
 * 
 */


function load_user_information(){        //加载用户信息
	var $user_nickname = $("#form-field-username");   //用户昵称
	var $user_sex_man = $("#man");
	var $user_sex_woman = $("#woman");
	var $user_hobby = $("#form-hobby-first");
	var $user_birthday = $("#form-field-date");
	var $user_birth_place = $("#form-place-first");
	var $user_qq = $("#form-field-qq");
	var $user_introduction = $("#form-field-comment");
	 $.get("User_data",function(data,status){
		  if(data.result==true){
			  
			  if(data.user_nickname!=null){
				  $user_nickname.val(data.user_nickname);
			  }
			  
			  if(data.user_sex!=null){
				  if(data.user_sex=="男"){
					  $user_sex_man[0].checked = true;
				  }else if(data.user_sex=="女"){
					  $user_sex_woman[0].checked = true;
				  }
			  }
			  
			  if(data.user_hobby!=null){
				  $user_hobby.val(data.user_hobby);
			  }
			  
			  if(data.user_birthday!=null){
				  $user_birthday.val(data.user_birthday);
			  }
			 
			  if(data.user_birth_place!=null){
			  $user_birth_place.val(data.user_birth_place);
			  }
			  
			  if(data.user_qq!=null){
				  $user_qq.val(data.user_qq);
			  }
			  
			  if(data.user_introduction!=null){
				  $user_introduction.val(data.user_introduction);
			  }
			  
		  }else{
			  alert("出错");
		  }
	  },"json");
};



$(function(){
	load_user_information();
});






/*$(function(){
	var $user_photo_file = $("#user_photo_file");  //用户头像文件
	var $user_nickname = $("#form-field-username");   //用户昵称
	var $user_sex_man = $("#man");
	var $user_sex_woman = $("#woman");
	var $user_hobby = $("#form-hobby-first");
	var $user_birthday = $("#form-field-date");
	var $user_birth_place = $("#form-place-first");
	var $user_qq = $("#form-field-qq");
	var $user_introduction = $("#form-field-comment");
	
	$user_photo_file.change(function(){    //文件改变时
		var file = $(this).val();      //图片名称
		//alert(file);
		var suffix_index = file.lastIndexOf(".");
		var suffix_str = file.substring(suffix_index,file.length).toLocaleLowerCase();  //转换为小写
		
		 if(suffix_str!=".bmp"&&suffix_str!=".png"&&suffix_str!=".gif"&&suffix_str!=".jpg"&&suffix_str!=".jpeg"){
			 alert("图片限于bmp,png,gif,jpeg,jpg格式");
			 return false;
		 }else{
			 var image = this.files[0];
				if(image.size>1024*150){
					alert("图片大小不能超过150kb");
					return false;
				}
		 }
	});
	
	
	
});*/

//////////////-------------------用户数据修改------------------/////////
$(function(){
	var $user_information_edit_save = $("button.user_information_edit_save");
	var $user_information_edit_reset = $("button.user_information_edit_reset");
	
	$user_information_edit_save.click(function(){ //保存用户信息
		//$("form.user_data_submit")[0].submit();        //会跳转页面
		var height = $(document).scrollTop();         //滚动条距离顶部的高度
		var $loading = $("div.la-ball-atom");
		$loading.css("top",height+150);
		$loading.show(500);
		$("form.user_data_submit").ajaxSubmit({
			url:"User_data_edit",
			type:"post",
			dataType:"json",
			async:false,
			success:function(data){
				if(data.result==true){
					$loading.hide(500);
				}else{
					alert("保存信息失败");
					$loading.hide(500);
				}
		}});
	});
	
	$user_information_edit_reset.click(function(){    //重置用户信息
		load_user_information();
	});
	
});


////////////////---------------------密码修改----------------////////////
$(function(){
	var $form_field_pass1 = $("#form-field-pass1");
	var $form_field_pass2 = $("#form-field-pass2");
	var $form_field_pass3 = $("#form-field-pass3");
	var $password_modify = $("button.password_modify");
	
	$password_modify.click(function(){
		var form_field_pass1 = $form_field_pass1.val();
		var form_field_pass2 = $form_field_pass2.val();
		var form_field_pass3 = $form_field_pass3.val();
		
		if(form_field_pass2==form_field_pass3&&form_field_pass2.length>7){
			$.post("Password_modify",{"old_user_password":$.md5(form_field_pass1),"new_user_password":$.md5(form_field_pass2)},function(data,status){
				if(data.result==true){
					//修改成功
					jSuccess("密码已修改",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
				}else{
					//修改失败
					jError("密码修改失败，请确认输入是否正确",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
				}
			},"json");
		}else{
			jError("请再次确认密码，两次密码不一致或格式不符合",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
		}

	});
});


////////////////////////------------------绑定手机/邮箱读取----------------/////////////
$(function(){
	var $old_phone = $("#binded_phone");
	var $old_email = $("#binded_email");
	$.get("User_land_data",function(data,status){
		if(data.result==true){
			if(data.user_telephone!=""&&data.user_telephone!=null){
				$old_phone.html(data.user_telephone);
			}else{
				$old_phone.html("暂未绑定任何手机");
			}
			if(data.user_email!=""&&data.user_email!=null){
				$old_email.html(data.user_email);
			}else{
				$old_email.html("暂未绑定任何邮箱");
			}
			
		}
	},"json");
});



//////////////////////////---------------绑定手机号码-----------------///////////////

/**************电话号码验证***********************/
function phone(text){
	var regex = /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
	if(regex.test(text)&text!=""){
		return true;
	}
	else{
		return false;
	}
}

$(function(){
	var $old_phone = $("#binded_phone");
	var $new_phone = $("#form-field-phone");
	var $verification_code = $("#verification");
	
	var $send_msg = $("#send_msg");
	
	var verification_code = "";   
	
	$send_msg.click(function(){
		var new_phone = $new_phone.val();
		if(phone(new_phone)){
		//倒计时
		$(this)[0].disabled=true;
		 var time = 60;                            //定时器
		$(this).html(time+"秒内验证码有效");
		timer = setInterval(function(){
			 time = time-1;
			 $("#send_msg").html(time+"秒内验证码有效");
			 if(time==0){                            //60秒之后
				 clearInterval(timer);                //关闭定时器
				 $("#send_msg")[0].disabled=false;    //取消禁用
				 $("#send_msg").html("<i class='icon-undo bigger-110'></i>发送验证码");
				 time = 60;
				 verification_code = "";                   //重新设置verification的值
				 $save_phone.unbind("input propertychange");  //移除事件
			 }
		 },1000);
		//end倒计时
		
		 $.ajax({        //ajax请求发送短信，返回发送结果
			 url:"Phone_modify_msg_send",
			 type:"POST",
			 data:{"new_phone":new_phone},
			 async:false,
			 dataType:"json",
			 success:function(data,datastatus){
				 eval("res = "+data.re+";");
				 try{
				 if(res.alibaba_aliqin_fc_sms_num_send_response.result.success){
				     verification_code = data.verification_code;  //获取到的验证码		
				     //短信发送成功
				     $verification_code.bind("input propertychange",function(){
				    	 var input_verification_code = $(this).val();
				    	 var $save_phone = $("#save_phone");
				    	 if(input_verification_code.length>=5){
				    		 if(input_verification_code==verification_code){
				    			 //对
				    			 if($(this).siblings("i.icon-ok").hasClass("hidden")){
				    				 $(this).siblings("i.icon-ok").removeClass("hidden");
				    			 }else{
				    				 //
				    			 }
				    			 
				    			 if($(this).siblings("i.icon-remove").hasClass("hidden")){
				    				 //
				    			 }else{
				    				 $(this).siblings("i.icon-remove").addClass("hidden");
				    			 }
				    			 	
				    				$save_phone.click(function(){
				    					$.post("Phone_modify",{"new_phone":new_phone, "verification_code":verification_code},function(data,status){
				    						if(data.result==true){
				    							//成功
				    							jSuccess("绑定手机成功",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
				    						}else{
				    							//失败
				    							alert("绑定手机失败");
				    							jError("绑定手机失败",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
				    						}
				    					},"json");
				    				});
				    		 }else{
				    			 //错
				    			 if($(this).siblings("i.icon-ok").hasClass("hidden")){
				    				 //
				    			 }else{
				    				 $(this).siblings("i.icon-ok").addClass("hidden");
				    			 }
				    			 
				    			 if($(this).siblings("i.icon-remove").hasClass("hidden")){
				    				 $(this).siblings("i.icon-remove").removeClass("hidden");
				    			 }else{
				    				//
				    			 }
				    			 
				    			 
				    			 $save_phone.unbind("click");  //移除事件
				    		 }
				    	 }
				     });

					}else{
						jError("短信发送失败,请等待一定时间后再尝试",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
					}
				 }catch(e){
					 jError("短信发送失败,请等待一定时间后再尝试",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
				 }
			 }
		 });
		
		
	}else{
		jError("手机号码不正确",{HorizontalPosition : "center",VerticalPosition : "center",ShowOverlay : false});
	}
	});
	

});